<?php
/*
 *资讯文章
*/
namespace App\Http\Controllers;

use App\Service\NewsOrigService;
use App\Validation\ListValidation;
use App\Validation\NewsListValidation;

use App\Service\MemberPraiseService;
use App\Service\MemberCollectService;
use App\Service\ConfigsService;
use App\Util\Fun;

class NewsOrigController extends BaseController
{
	const DISPLAY_TRUE = 1;		//显示标识：是
	const DISPLAY_FALSE = 0;	//显示标识：否
	const COLLECT_TRUE = 1;		//收藏标识：是
	const COLLECT_FALSE = 0;	//收藏标识：否


	/**
	 * [newsAction 原创+实时新闻列表 2017-10-26+]
	 * @sub_type [2市场，3公司，4土地，5导购]
	 * @return [type] [description]
	 */
	public function newsAction()
	{
		$this->getUser(0);

		$raw = $this->validation(new NewsListValidation, $this->request->getJsonRawBody());
		$raw->city_id = $this->city_id;
		$raw->is_display = self::DISPLAY_TRUE;
		$raw->member_id = property_exists($raw,'member_id') ? $raw->member_id : 0;
		$data = NewsOrigService::getNewsAll($raw);
		
		$d = [];
		foreach ($data as $key => $value) {
			unset($value->sub_type);
			$d[$value->id] = $value;

			//2017-11-06 + 列表展示摘要
			$summary = "";
			$preg = "|(.*)。|isU";
	    	$size = 200;
	    	if ( $value->content && trim($value->content) ){
	    		$summary = self::summary($value->content, $size);
	    		preg_match($preg,strip_tags($summary),$match);
	    		$summary = current($match) ? $match[0] : "";
	    	}
	    	$d[$value->id]->summary = trim($value->summary) ? trim($value->summary) : $summary;
	    	//2017-11-06 +

			//判断缩略图是否有图且url是否完整
			$d[$value->id]->thumb = $value->thumb ? $this->getFullUrl($value->thumb, FALSE, $this->config->imgurl) : $this->config->imgurl . '/uploads/default.png';

			$d[$value->id]->comment = $value->comment > 10000 ? floor($value->comment / 10000) . '万+' : $value->comment;
			$d[$value->id]->visit = $value->visit > 10000 ? floor($value->visit / 10000) . '万+' : $value->visit;
			$d[$value->id]->sources = $value->sources ? $value->sources : "觅窝";

			if ($value->groupId == 10)
				$d[$value->id]->sources = "觅窝";

			if (strtotime($value->create_time) < strtotime(date("Y-m-d", time())) && $value->tags == 'new')
				$value->tags = '';
			if ($value->groupId == 2){
				//实时新闻纯文字
				if ($value->type_id == 1){
					$value->type_id = 3;
				}else{
					$value->type_id = 1;
				}
			}

			$d[$value->id]->create_time = $this->getShowTime($value->create_time);
			$d[$value->id]->tags = $value->tags ? $value->tags : "";
			$d[$value->id]->tags = $value->tags == "topic" ? "" : $value->tags;

			$d[$value->id]->is_praise  = (string)self::COLLECT_FALSE;
			$d[$value->id]->is_collect = (string)self::COLLECT_FALSE;
			$is_praise = $d[$value->id]->is_praise ? 1 : 0;

			//原创新闻go_url
			$d[$value->id]->go_url = $this->config->gourl . $this->config->urls->news_orig . $value->id;
			//实时新闻go_url
			if ($value->groupId == 2)
				$d[$value->id]->go_url = $this->config->gourl . $this->config->urls->news . $value->id;
			//原创新闻专题go_url
			if ($value->topic_sign == 1)
				$d[$value->id]->go_url = $this->config->gourl . $this->config->urls->news_topic . $value->id;
		}

		$d = array_values($d);

		$count = NewsOrigService::getNewsAll($raw, TRUE);
		$assign = [
			'total' => intval($count[0]->counts),
			'list'  => $d
		];
		return $this->echoJson($assign);
	}

	/**
	 * [indexAction 列表]
	 * @return [type] [description]
	 */
    public function indexAction()
    {
    	$this->getUser(0);

		$raw = $this->validation(new NewsListValidation, $this->request->getJsonRawBody());
		$raw->city_id = $this->city_id;
		$raw->is_display = self::DISPLAY_TRUE;
		$raw->member_id = property_exists($raw,'member_id') ? $raw->member_id : 0;
		//默认获取普通新闻
		$raw->news_type = property_exists($raw,'news_type') ? $raw->news_type : 'putong';

		// $data = NewsOrigService::getList($raw);
		if (isset($raw->target_id) && $raw->target_id){
			$data = NewsOrigService::getRecoList($raw);
		}else{
			$data_putong = NewsOrigService::getNewsList($raw);
			if ($raw->p == 1 && $raw->news_type == 'putong'){//当获取第一页时，获取21条数据，第二条为最新的项目新闻
				$raw->news_type = 'xiangmu';
				$raw->page_num = 1;
				$data_xiangmu = NewsOrigService::getNewsList($raw);
				//2017-10-23将项目新闻替换为专题新闻
				$data_xiangmu = [];
				$data = [];
				foreach ($data_putong as $key => $value) {
					if ($key == 0){
						$data[$key] = $value;
					}else if ($key == 1 && count($data_xiangmu) > 0){
						//项目新闻最新的放在第二位
						$data[1] = current($data_xiangmu);
						$data[2] = $value;
					}else{
						$data[$key +1] = $value;
					}
				}
			$data = array_values($data);
			}else{//当不是第一页是，获取20条
				$data = $data_putong;
			}
		}
		// $data = NewsOrigService::getNewsList($raw);
		//获取点赞和收藏
		$praise_data = [];
		$collect_data = [];
		if ($raw->member_id && $raw->member_id >0 && count($data) > 0){
			$groupId = 10;
			// $groupId = NewsOrigService::getTableId();
			$id_arr = array_map('parent::getIdArray',$data);
			$praise_data  = MemberPraiseService::jugePraiseArr($raw->member_id, $groupId, $id_arr);
			$collect_data = MemberCollectService::jugeCollectArr($raw->member_id, $groupId, $id_arr);
		}

		$d = [];
		
		foreach ($data as $key => $value) {
			unset($value->index_by);
			unset($value->reco_by);

			$d[$value->id] = $value;

			//2017-11-06 + 列表展示摘要
			$summary = "";
			$preg = "|(.*)。|isU";
	    	$size = 200;
	    	if ( $value->content && trim($value->content) ){
	    		$summary = self::summary($value->content, $size);
	    		preg_match($preg,strip_tags($summary),$match);
	    		$summary = current($match) ? $match[0] : "";
	    	}
	    	$d[$value->id]->summary = trim($value->summary) ? trim($value->summary) : $summary;
	    	//2017-11-06 +
	    	
			//判断缩略图是否有图且url是否完整
			$d[$value->id]->thumb = $value->thumb ? $this->getFullUrl($value->thumb, FALSE, $this->config->imgurl) : $this->config->imgurl . '/uploads/default.png';

			$d[$value->id]->comment = $value->comment > 10000 ? floor($value->comment / 10000) . '万+' : $value->comment;
			$d[$value->id]->visit = $value->visit > 10000 ? floor($value->visit / 10000) . '万+' : $value->visit;
			$d[$value->id]->sources = "觅窝";

			if (strtotime($value->create_time) < strtotime(date("Y-m-d", time())) && $value->tags == 'new')
				$value->tags = '';

			$d[$value->id]->create_time = $this->getShowTime($value->create_time);
			$d[$value->id]->tags = $value->tags ? $value->tags : "";
			$d[$value->id]->tags = $value->tags == "topic" ? "" : $value->tags;

			$d[$value->id]->is_praise  = in_array($value->id, $praise_data) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;
			$d[$value->id]->is_collect = in_array($value->id, $collect_data) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;

			$is_praise = $d[$value->id]->is_praise ? 1 : 0;
			$d[$value->id]->go_url = $this->config->gourl . $this->config->urls->news_orig . $value->id . '/' . $value->groupId . '/' . $is_praise;
			$d[$value->id]->go_url = $this->config->gourl . $this->config->urls->news_orig . $value->id;
			if ($value->topic_sign == 1)
				$d[$value->id]->go_url = $this->config->gourl . $this->config->urls->news_topic . $value->id;

			$d[$value->id]->topic = [];
			if ($value->topic_id > 0){
				$topic = NewsOrigService::getList($raw, FALSE , $value->topic_id);

				//获取点赞和收藏
				$topic_praise_data = [];
				$topic_collect_data = [];
				if ($raw->member_id && $raw->member_id >0 && count($topic->toArray()) > 0){
					$topic_id_arr = array_map('parent::getIdArray',$topic->toArray());
					$topic_praise_data  = MemberPraiseService::jugePraiseArr($raw->member_id, $groupId, $topic_id_arr);
					$topic_collect_data = MemberCollectService::jugeCollectArr($raw->member_id, $groupId, $topic_id_arr);
				}
				$t_d = [];
				foreach ($topic as $kk => $vv) {
				 	unset($vv->index_by);
					unset($vv->reco_by);
					$d[$value->id]->topic[$vv->id] = $vv;

					//2017-11-06 + 列表展示摘要
					$summary = "";
					$preg = "|(.*)。|isU";
	    			$size = 200;
			    	if ( $vv->content && trim($vv->content) ){
			    		$summary = self::summary($vv->content, $size);
			    		preg_match($preg,strip_tags($summary),$match);
			    		$summary = current($match) ? $match[0] : "";
			    	}
			    	$d[$value->id]->topic[$vv->id]->summary = trim($vv->summary) ? trim($vv->summary) : $summary;
			    	//2017-11-06 +

				 	//判断缩略图是否有图且url是否完整
					$d[$value->id]->topic[$vv->id]->thumb = $vv->thumb ? $this->getFullUrl($vv->thumb, FALSE, $this->config->imgurl) : $this->config->imgurl . '/uploads/default.png';

					$d[$value->id]->topic[$vv->id]->comment = $vv->comment > 10000 ? floor($vv->comment / 10000) . '万+' : $vv->comment;
					$d[$value->id]->topic[$vv->id]->visit = $vv->visit > 10000 ? floor($vv->visit / 10000) . '万+' : $vv->visit;
					$d[$value->id]->topic[$vv->id]->sources = "觅窝";

					if (strtotime($vv->create_time) < strtotime(date("Y-m-d", time())) && $vv->tags == 'new')
						$vv->tags = '';

					$d[$value->id]->topic[$vv->id]->create_time = $this->getShowTime($vv->create_time);
					$d[$value->id]->topic[$vv->id]->tags = $vv->tags ? $vv->tags : "";

					$d[$value->id]->topic[$vv->id]->is_praise  = in_array($vv->id, $topic_praise_data) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;
					$d[$value->id]->topic[$vv->id]->is_collect = in_array($vv->id, $topic_collect_data) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;

					$is_praise = $d[$value->id]->topic[$vv->id]->is_praise ? 1 : 0;
					$d[$value->id]->topic[$vv->id]->go_url = $this->config->gourl . $this->config->urls->video . $vv->id . '/' . $vv->groupId . '/' . $is_praise;
					unset($d[$value->id]->topic[$vv->id]->topic_id);
					unset($d[$value->id]->topic[$vv->id]->topic_group);
				}
				$d[$value->id]->topic = array_values($d[$value->id]->topic);
			}

			unset($d[$value->id]->topic_id);
			unset($d[$value->id]->topic_group);
		}

		// $d = array_replace($d,$arr);
		$d = array_values($d);
		$count = NewsOrigService::getList($raw, TRUE);
		$assign = [
			'total' => $count,
			'list'  => $d
		];
		return $this->echoJson($assign);
    }

    /**
	 * [indexAction 列表]
	 * @return [type] [description]
	 */
    public function tlistAction()
    {
    	$this->getUser(0);

		$raw = $this->validation(new NewsListValidation, $this->request->getJsonRawBody());
		$raw->city_id = $this->city_id;
		$raw->is_display = self::DISPLAY_TRUE;
		$raw->member_id = property_exists($raw,'member_id') ? $raw->member_id : 0;
		//默认获取普通新闻
		$raw->news_type = property_exists($raw,'news_type') ? $raw->news_type : 'putong';

		if (isset($raw->target_id) && $raw->target_id){
			$data = NewsOrigService::getTopicRecoList($raw);
		}else{
			$data = NewsOrigService::getTopicList($raw);
		}

		//获取点赞和收藏
		$praise_data = [];
		$collect_data = [];
		if ($raw->member_id && $raw->member_id >0 && count($data) > 0){
			$groupId = 10;
			// $groupId = NewsOrigService::getTableId();
			$id_arr = array_map('parent::getIdArray',$data);
			$praise_data  = MemberPraiseService::jugePraiseArr($raw->member_id, $groupId, $id_arr);
			$collect_data = MemberCollectService::jugeCollectArr($raw->member_id, $groupId, $id_arr);
		}

		$d = [];
		
		foreach ($data as $key => $value) {
			unset($value->index_by);
			unset($value->reco_by);

			$d[$value->id] = $value;

			//2017-11-06 + 列表展示摘要
			$summary = "";
			$preg = "|(.*)。|isU";
	    	$size = 200;
	    	if ( $value->content && trim($value->content) ){
	    		$summary = self::summary($value->content, $size);
	    		preg_match($preg,strip_tags($summary),$match);
	    		$summary = current($match) ? $match[0] : "";
	    	}
	    	$d[$value->id]->summary = trim($value->summary) ? trim($value->summary) : $summary;
	    	//2017-11-06 +

			//判断缩略图是否有图且url是否完整
			$d[$value->id]->thumb = $value->thumb ? $this->getFullUrl($value->thumb, FALSE, $this->config->imgurl) : $this->config->imgurl . '/uploads/default.png';

			$d[$value->id]->comment = $value->comment > 10000 ? floor($value->comment / 10000) . '万+' : $value->comment;
			$d[$value->id]->visit = $value->visit > 10000 ? floor($value->visit / 10000) . '万+' : $value->visit;
			$d[$value->id]->sources = "觅窝";

			if (strtotime($value->create_time) < strtotime(date("Y-m-d", time())) && $value->tags == 'new')
				$value->tags = '';

			$d[$value->id]->create_time = $this->getShowTime($value->create_time);
			$d[$value->id]->tags = $value->tags ? $value->tags : "";

			$d[$value->id]->is_praise  = in_array($value->id, $praise_data) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;
			$d[$value->id]->is_collect = in_array($value->id, $collect_data) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;

			$is_praise = $d[$value->id]->is_praise ? 1 : 0;
			$d[$value->id]->go_url = $this->config->gourl . $this->config->urls->news_orig . $value->id . '/' . $value->groupId . '/' . $is_praise;
			$d[$value->id]->go_url = $this->config->gourl . $this->config->urls->news_orig . $value->id;
			if ($value->topic_sign == 1)
				$d[$value->id]->go_url = $this->config->gourl . $this->config->urls->news_topic . $value->id;

			$d[$value->id]->topic = [];

			unset($d[$value->id]->topic_id);
			unset($d[$value->id]->topic_group);
		}

		// $d = array_replace($d,$arr);
		$d = array_values($d);
		$count = NewsOrigService::getTopicList($raw, TRUE);

		$assign = [
			'total' => intval($count[0]->counts),
			'list'  => $d
		];
		return $this->echoJson($assign);
    }

	/**
	 * [indexAction 列表]
	 * @return [type] [description]
	 */
    /*public function testAction()
    {
    	$this->getUser(0);

		$raw = $this->validation(new ListValidation, $this->request->getJsonRawBody());
		$raw->city_id = $this->city_id;
		$raw->is_display = self::DISPLAY_TRUE;
		$raw->member_id = property_exists($raw,'member_id') ? $raw->member_id : 0;
		//默认获取普通新闻
		$raw->news_type = property_exists($raw,'news_type') ? $raw->news_type : 'putong';

		// $data = NewsOrigService::getList($raw);
		if (isset($raw->target_id) && $raw->target_id){
			$data = NewsOrigService::getRecoList($raw);
		}else{
			$data_putong = NewsOrigService::getNewsList($raw);
			if ($raw->p == 1 && $raw->news_type == 'putong'){//当获取第一页时，获取21条数据，第二条为最新的项目新闻
				$raw->news_type = 'xiangmu';
				$raw->page_num = 1;
				$data_xiangmu = NewsOrigService::getNewsList($raw);
				$data = [];
				foreach ($data_putong as $key => $value) {
					if ($key == 0){
						$data[$key] = $value;
					}else if ($key == 1 && count($data_xiangmu) > 0){
						//项目新闻最新的放在第二位
						$data[1] = current($data_xiangmu);
						$data[2] = $value;
					}else{
						$data[$key +1] = $value;
					}
				}
			$data = array_values($data);
			}else{//当不是第一页是，获取20条
				$data = $data_putong;
			}
		}
		// $data = NewsOrigService::getNewsList($raw);
		//获取点赞和收藏
		$praise_data = [];
		$collect_data = [];
		if ($raw->member_id && $raw->member_id >0 && count($data) > 0){
			$groupId = 10;
			// $groupId = NewsOrigService::getTableId();
			$id_arr = array_map('parent::getIdArray',$data);
			$praise_data  = MemberPraiseService::jugePraiseArr($raw->member_id, $groupId, $id_arr);
			$collect_data = MemberCollectService::jugeCollectArr($raw->member_id, $groupId, $id_arr);
		}

		$d = [];
		
		foreach ($data as $key => $value) {
			unset($value->index_by);
			unset($value->reco_by);

			$d[$value->id] = $value;

			//判断缩略图是否有图且url是否完整
			$d[$value->id]->thumb = $value->thumb ? $this->getFullUrl($value->thumb, FALSE, $this->config->imgurl) : $this->config->imgurl . '/uploads/default.png';

			$d[$value->id]->comment = $value->comment > 10000 ? floor($value->comment / 10000) . '万+' : $value->comment;
			$d[$value->id]->visit = $value->visit > 10000 ? floor($value->visit / 10000) . '万+' : $value->visit;
			$d[$value->id]->sources = "觅窝";

			if (strtotime($value->create_time) < strtotime(date("Y-m-d", time())) && $value->tags == 'new')
				$value->tags = '';

			$d[$value->id]->create_time = $this->getShowTime($value->create_time);
			$d[$value->id]->tags = $value->tags ? $value->tags : "";

			$d[$value->id]->is_praise  = in_array($value->id, $praise_data) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;
			$d[$value->id]->is_collect = in_array($value->id, $collect_data) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;

			$is_praise = $d[$value->id]->is_praise ? 1 : 0;
			$d[$value->id]->go_url = $this->config->gourl . $this->config->urls->news_orig . $value->id . '/' . $value->groupId . '/' . $is_praise;
			$d[$value->id]->go_url = $this->config->gourl . $this->config->urls->news_orig . $value->id;

			$d[$value->id]->topic = [];
			if ($value->topic_id > 0){
				$topic = NewsOrigService::getList($raw, FALSE , $value->topic_id);

				//获取点赞和收藏
				$topic_praise_data = [];
				$topic_collect_data = [];
				if ($raw->member_id && $raw->member_id >0 && count($topic->toArray()) > 0){
					$topic_id_arr = array_map('parent::getIdArray',$topic->toArray());
					$topic_praise_data  = MemberPraiseService::jugePraiseArr($raw->member_id, $groupId, $topic_id_arr);
					$topic_collect_data = MemberCollectService::jugeCollectArr($raw->member_id, $groupId, $topic_id_arr);
				}
				$t_d = [];
				foreach ($topic as $kk => $vv) {
				 	unset($vv->index_by);
					unset($vv->reco_by);
					$d[$value->id]->topic[$vv->id] = $vv;
				 	//判断缩略图是否有图且url是否完整
					$d[$value->id]->topic[$vv->id]->thumb = $vv->thumb ? $this->getFullUrl($vv->thumb, FALSE, $this->config->imgurl) : $this->config->imgurl . '/uploads/default.png';

					$d[$value->id]->topic[$vv->id]->comment = $vv->comment > 10000 ? floor($vv->comment / 10000) . '万+' : $vv->comment;
					$d[$value->id]->topic[$vv->id]->visit = $vv->visit > 10000 ? floor($vv->visit / 10000) . '万+' : $vv->visit;
					$d[$value->id]->topic[$vv->id]->sources = "觅窝";

					if (strtotime($vv->create_time) < strtotime(date("Y-m-d", time())) && $vv->tags == 'new')
						$vv->tags = '';

					$d[$value->id]->topic[$vv->id]->create_time = $this->getShowTime($vv->create_time);
					$d[$value->id]->topic[$vv->id]->tags = $vv->tags ? $vv->tags : "";

					$d[$value->id]->topic[$vv->id]->is_praise  = in_array($vv->id, $topic_praise_data) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;
					$d[$value->id]->topic[$vv->id]->is_collect = in_array($vv->id, $topic_collect_data) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;

					$is_praise = $d[$value->id]->topic[$vv->id]->is_praise ? 1 : 0;
					$d[$value->id]->topic[$vv->id]->go_url = $this->config->gourl . $this->config->urls->video . $vv->id . '/' . $vv->groupId . '/' . $is_praise;
					unset($d[$value->id]->topic[$vv->id]->topic_id);
					unset($d[$value->id]->topic[$vv->id]->topic_group);
				}
				$d[$value->id]->topic = array_values($d[$value->id]->topic);
			}

			unset($d[$value->id]->topic_id);
			unset($d[$value->id]->topic_group);
		}

		// $d = array_replace($d,$arr);
		$d = array_values($d);
		$count = NewsOrigService::getList($raw, TRUE);
		$assign = [
			'total' => $count,
			'list'  => $d
		];
		return $this->echoJson($assign);
    }*/



	/**
	 * [detailAction 详情]
	 * @return [type] [description]
	 */
    public function detailAction()
    {
    	$this->getUser(0);
        $image_water = ConfigsService::getKeyVal('image_water');
        $water_pos = [
            1=>'nw',
            2=>'north',
            3=>'ne',
            4=>'west',
            5=>'center',
            6=>'east',
            7=>'sw',
            8=>'south',
            9=>'se',
        ];
		$id = $this->dispatcher->getParam('id');
		if (!$id)
			return $this->echoJson();
		
		if (isset($this->user)){
			$data = NewsOrigService::getInfo($id, $this->user->member_id);
		}else{
			$data = NewsOrigService::getInfo($id);
		}
		if (is_object($data)){
			//判断缩略图是否有图且url是否完整
			$data->thumb = $data->thumb ? $this->getFullUrl($data->thumb, FALSE, $this->config->imgurl) : $this->config->imgurl . '/uploads/default.png';
			$data->pics = explode(',', $data->pics);
			$data->pics = array_filter($data->pics);
			$data->content = $data->content ? $data->content : "";
			$data->go_url = $this->config->gourl . $this->config->urls->news_orig . $data->id;
			$data->comment = $data->comment > 10000 ? floor($data->comment / 10000) . '万+' : $data->comment;
			$data->visit = $data->visit > 10000 ? floor($data->visit / 10000) . '万+' : $data->visit;
			$data->sources = "觅窝·新媒体";
			$preg = "|(.*)。|isU";
	    	$size = 200;
	    	$summary = "";
	    	if ( $data->content && trim($data->content) ){
                //
                $images = (new Fun)->getAllTextImages($data->content);
                //?x-oss-process=image/resize,w_50
                $watemark = base64_encode(str_replace('http://res.15miwo.cn/','',$image_water->watermark) .'?x-oss-process=image/resize,w_120');
            
                $new_images = [];
                foreach($images as $img_url){
                    $new_images[] = $img_url ."?x-oss-process=image/resize,w_640/watermark,image_{$watemark},t_60,g_{$water_pos[$image_water->waterPos]}";
                }
                $images[] = '请点击写图片名称';
                $new_images[] = '';
                $data->content = str_replace($images,$new_images,$data->content);
	    		$summary = self::summary($data->content, $size);
	    		preg_match($preg,strip_tags($summary),$match);
	    		$summary = current($match) ? $match[0] : "";
	    	}
	    	$data->summary = trim($data->summary) ? trim($data->summary) : $summary;
			unset($data->is_self);
			unset($data->index_by);
			unset($data->reco_by);
			// unset($data->is_display);
			unset($data->is_del);
			unset($data->city_id);
			unset($data->topic_id);
			unset($data->type_id);
			unset($data->order_by);
			// unset($data->create_time);
		}			

		return  $this->echoJson($data);
    }

    public function topicAction()
    {
    	$this->getUser(0);

		$id = $this->dispatcher->getParam('id');
		if (!$id)
			return $this->echoJson();
		
		if (isset($this->user)){
			$data = NewsOrigService::getInfo($id, $this->user->member_id);
		}else{
			$data = NewsOrigService::getInfo($id);
		}
		$arr = [];
		if (is_object($data)){
			//判断缩略图是否有图且url是否完整
			$data->thumb = $data->thumb ? $this->getFullUrl($data->thumb, FALSE, $this->config->imgurl) : $this->config->imgurl . '/uploads/default.png';
			$data->pics = explode(',', $data->pics);
			$data->pics = array_filter($data->pics);
			$data->content = $data->content ? $data->content : "";
			$data->go_url = $this->config->gourl . $this->config->urls->news_orig . $data->id;
			if ($data->topic_sign == 1)
				$data->go_url = $this->config->gourl . $this->config->urls->news_topic . $data->id;
			$data->comment = $data->comment > 10000 ? floor($data->comment / 10000) . '万+' : $data->comment;
			$data->visit = $data->visit > 10000 ? floor($data->visit / 10000) . '万+' : $data->visit;
			$data->sources = "觅窝·新媒体";
			$preg = "|(.*)。|isU";
	    	$size = 200;
	    	$summary = self::summary($data->content, $size);
	    	preg_match($preg,strip_tags($summary),$match);
	    	$summary = current($match) ? $match[0] : "";
	    	$data->summary = trim($data->summary) ? trim($data->summary) : $summary;
			unset($data->is_self);
			unset($data->index_by);
			unset($data->reco_by);
			// unset($data->is_display);
			unset($data->is_del);
			unset($data->city_id);
			unset($data->topic_id);
			unset($data->type_id);
			unset($data->order_by);
			// unset($data->create_time);
		}
		$arr = (array)$data;
		$topics = [];
		if (is_object($data)){
			if ($data->topic_sign == 1){
				$topic_ar = NewsOrigService::getTopic($id);
				if (is_array($topic_ar) && count($topic_ar) > 0){
					foreach ($topic_ar as $kk => $topic) {
						$d = [];
						$news_list = NewsOrigService::getNewsListIds($topic->ids);
						if (count($news_list) <= 0)
							return ;
						foreach ($news_list as $key => $value) {
							unset($value->index_by);
							unset($value->reco_by);
							unset($value->topic);
							$d[$value->id] = $value;

							//2017-11-06 + 列表展示摘要
							$summary = "";
							$preg = "|(.*)。|isU";
	    					$size = 200;
					    	if ( $value->content && trim($value->content) ){
					    		$summary = self::summary($value->content, $size);
					    		preg_match($preg,strip_tags($summary),$match);
					    		$summary = current($match) ? $match[0] : "";
					    	}
					    	$d[$value->id]->summary = trim($value->summary) ? trim($value->summary) : $summary;
					    	//2017-11-06 +
	    	
							//判断缩略图是否有图且url是否完整
							$d[$value->id]->thumb = $value->thumb ? $this->getFullUrl($value->thumb, FALSE, $this->config->imgurl) : $this->config->imgurl . '/uploads/default.png';

							$d[$value->id]->comment = $value->comment > 10000 ? floor($value->comment / 10000) . '万+' : $value->comment;
							$d[$value->id]->visit = $value->visit > 10000 ? floor($value->visit / 10000) . '万+' : $value->visit;
							$d[$value->id]->sources = "觅窝·新媒体";

							if (strtotime($value->create_time) < strtotime(date("Y-m-d", time())) && $value->tags == 'new')
								$value->tags = '';

							$d[$value->id]->create_time = $this->getShowTime($value->create_time);
							$d[$value->id]->tags = $value->tags ? $value->tags : "";

							// $d[$value->id]->is_praise  = in_array($value->id, $praise_data) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;
							// $d[$value->id]->is_collect = in_array($value->id, $collect_data) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;
							$d[$value->id]->is_praise  = (string)self::COLLECT_FALSE;
							$d[$value->id]->is_collect = (string)self::COLLECT_FALSE;
							$is_praise = $d[$value->id]->is_praise ? 1 : 0;
							$d[$value->id]->go_url = $this->config->gourl . $this->config->urls->news_orig . $value->id . '/' . $value->groupId . '/' . $is_praise;
							$d[$value->id]->go_url = $this->config->gourl . $this->config->urls->news_orig . $value->id;
					    	$summary = self::summary($value->content, $size);
					    	preg_match($preg,strip_tags($summary),$match);
					    	$summary = current($match) ? $match[0] : "";
					    	$d[$value->id]->summary = trim($value->summary) ? trim($value->summary) : $summary;
							unset($d[$value->id]->content);
						}
						$topics[$kk]['program_name'] = $topic->program;
						$topics[$kk]['news_list'] = array_values($d);
					}

				}
				
			}
		}
		$arr['topics'] = array_values($topics);
		return  $this->echoJson($arr);
    }

    /**
     * [firstrecmdAction app首页获取推荐的资讯包括有：实时新闻，原创新闻，新房，二手房，住房，装修，视频]
     * @return [type] [description]
     */
    public function firstrecmdAction()
    {
        $para = $this->request->getJsonRawBody();

        //$type = $this->dispatcher->getParam('type');
        if ($para && isset($para->postion)){
            $postion=intval($para->postion)>0?intval($para->postion):1;
        }else{
            $postion=1;
        }
        if($this->city_id>0){
            $cityid="0,".$this->city_id;
        }else{
            $cityid="0";
        }
        $data=NewsOrigService::getFirstNewsRecmd(array('city_id'=>$cityid));
        $assign = [
            'total' => count($data),
            'list'  => $data
        ];
        return  $this->echoJson($assign);

    }

    /** 
	* 生成摘要 
	* @param (string) $body 
	*  正文 
	* @param (int) $size 
	*  摘要长度 
	* @param (int) $format 
	*  输入格式 id 
	*/   
	private function summary($body, $size, $format = NULL)
	{  
		$_size = mb_strlen($body, 'utf-8');  
		if($_size <= $size) 
			return $body;  
		// 输入格式中有 PHP 过滤器  
		// if(!isset($format) && filter_is_php($format))  
		//   return $body;  
	 	$strlen_var = strlen($body); 

	 	// 不包含 html 标签  
	 	if(strpos($body, '<') === false)
	 		return mb_substr($body, 0, $size);

	 	// 包含截断标志，优先  
	 	if($e = strpos($body, '<!-- break -->'))
	 		return mb_substr($body, 0, $e);  

	 	// html 代码标记  
	 	$html_tag = 0;  
	 	// 摘要字符串  
	 	$summary_string = '';  
		//数组用作记录摘要范围内出现的 html 标签 
		//开始和结束分别保存在 left 和 right 键名下
		//如字符串为：<h3><p><b>a</b></h3>，假设 p 未闭合
		//数组则为：array('left' => array('h3', 'p', 'b'), 'right' => 'b', 'h3'); 
		//仅补全 html 标签，<? <% 等其它语言标记，会产生不可预知结果 
	 	$html_array = ['left' => [], 'right' => []];  
	 	for($i = 0; $i < $strlen_var; ++$i) {  
		   	if(!$size) 
		   		break;  

	   		$current_var = substr($body, $i, 1);  
		   	if($current_var == '<'){  
		     	// html 代码开始  
		     	$html_tag = 1;  
		     	$html_array_str = '';

		   	}else if($html_tag == 1){  // 一段 html 代码结束 

		     	if($current_var == '>'){  //去除首尾空格，如 <br /  > < img src="" / > 等可能出现首尾空格 

			       	$html_array_str = trim($html_array_str);  //判断最后一个字符是否为 /，若是，则标签已闭合，不记录 
			        
			       	if(substr($html_array_str, -1) != '/'){ // 判断第一个字符是否 /，若是，则放在 right 单元 

		         		$f = substr($html_array_str, 0, 1); 

		         		if($f == '/'){ // 去掉 /  

		           			$html_array['right'][] = str_replace('/', '', $html_array_str);  
		  
		         		}else if($f != '?'){  // 判断是否为 ?，若是，则为 PHP 代码，跳过  

							//判断是否有半角空格，若有，以空格分割，第一个单元为 html 标签 如 <h2 class="a"> <p class="a"> 
			           		if(strpos($html_array_str, ' ') !== false){  
			  
			             	//分割成2个单元，可能有多个空格，如：<h2 class="" id="">  
			             	$html_array['left'][] = strtolower(current(explode(' ', $html_array_str, 2)));  
			  
			           		}else{ 

			           			//若没有空格，整个字符串为 html 标签，如：<b> <p> 等 // 统一转换为小写 
			             		$html_array['left'][] = strtolower($html_array_str);  
			           		}  
		  
		         		}  
		       		} 
		       		// 字符串重置  
		       		$html_array_str = '';  
		       		$html_tag = 0;  
		     	}else{  
		  			
		       		$html_array_str .= $current_var;//将< >之间的字符组成一个字符串 用于提取 html 标签   
		     	}  
		   	}else{  
		    	--$size;// 非 html 代码才记数    
		   	}  

	   		$ord_var_c = ord($body{$i});  
		   	switch (true) {  

		     	case (($ord_var_c & 0xE0) == 0xC0):  
		       		// 2 字节  
		       		$summary_string .= substr($body, $i, 2);  
		       		$i += 1;  
		     	break;  

		  		case (($ord_var_c & 0xF0) == 0xE0):  
		       		// 3 字节  
		       		$summary_string .= substr($body, $i, 3);  
		       		$i += 2;  
		     	break;  
		  
		     	case (($ord_var_c & 0xF8) == 0xF0):  
		       		// 4 字节  
		       		$summary_string .= substr($body, $i, 4);  
		       		$i += 3;  
		     	break;

		     	case (($ord_var_c & 0xFC) == 0xF8):  
		       		// 5 字节  
		       		$summary_string .= substr($body, $i, 5);  
		       		$i += 4;  
		     	break; 

		     	case (($ord_var_c & 0xFE) == 0xFC):  
		       		// 6 字节  
		       		$summary_string .= substr($body, $i, 6);  
		       		$i += 5;  
		     	break; 

		     	default:  
		       		// 1 字节  
		       		$summary_string .= $current_var;  
		   	}  
		  
		}  
 		//比对左右 html 标签，不足则补全  
		if($html_array['left']){  
		   /** 
		    * 交换 left 顺序，补充的顺序应与 html 出现的顺序相反 
		    * 如待补全的字符串为：<h2>abc<b>abc<p>abc 
		    * 补充顺序应为：</p></b></h2> 
		    */  
		 	$html_array['left'] = array_reverse($html_array['left']);  
		   	foreach($html_array['left'] as $index => $tag){  
		    	// 判断该标签是否出现在 right 中  
		    	$key = array_search($tag, $html_array['right']);  
		    	if($key !== false){  
		  			// 出现，从 right 中删除该单元  
		    		unset($html_array['right'][$key]);  
		    	}else{  
		   			// 没有出现，需要补全  
		    		$summary_string .= '</'.$tag.'>';  
		     	}  
		   	}  
		}  
	 	return $summary_string;  
	}
}